//检验表单输入
$(document).ready(function() {
    $("#signupForm").validate({
            rules: {
              username: {
                required: true,
                minlength: 2
              },
              password: {
                required: true,
                minlength: 6
              },
              confirm_password: {
                required: true,
                minlength: 6,
                equalTo: "#password"
              },
              email: {
                required: true,
                email: true
              },
              agree: {
                required: true
              }
            },
            messages: {
              username: {
                required: "请输入用户名",
                minlength: "用户名必需至少由两个字母组成"
              },
              password: {
                required: "请输入密码",
                minlength: "密码长度不能小于 6 个字母"
              },
              confirm_password: {
                required: "请输入密码",
                minlength: "密码长度不能小于 6 个字母",
                equalTo: "两次密码输入不一致"
              },
              email: "请输入一个正确的邮箱",
              agree: "请接受我们的用户协议"
            }
        });
});
var app = angular.module('myApp', []);

//修改Angular的$httpProvider的默认处理方式，否则无法在PHP里使用_POST[]获取数据
app.config(function($httpProvider){
    $httpProvider.defaults.transformRequest=function(obj){
        var str=[];
        for(var p in obj){
            str.push(encodeURIComponent(p)+"="+encodeURIComponent(obj[p]));
        }
        return str.join("&");
    };
    $httpProvider.defaults.headers.post={
        'Content-Type':'application/x-www-form-urlencoded'
    }
});

app.controller('signUpCtrl', function ($scope, $http) {

    //注册页面的重置功能
    $scope.reset = function(){
        $scope.email = "";
        $scope.username = "";
        $scope.password = "";
        $scope.confirm_password = "";
        $scope.isAgreed = false;
    }

    //注册功能
    $scope.register = function(){

        var email = $scope.email;
        var username = $scope.username;
        var password = $scope.password;
        if(username==null || username=="" || password==null || password=="")
            alert("请输入用户名密码！");
        else{
            if(!$scope.isAgreed){
                alert("未同意用户协议，无法完成注册！");
            }
            else{
                $http({
                            method:"post",
                            url: ip + "register.php",
                            data:{
                                email: email,
                                username: username,
                                password: password
                            }
                        }).success(function(data, status, headers, config){
                            if (data.success) {
                                //注册成功
                                alert("注册成功，请登录！");
                                document.getElementById("register_back_to_login").click();
                            }else{
                                alert("注册失败: " + data.msg);
                            }
                        })
            }
        }
    }

});

app.controller('loginCtrl', function ($scope, $http) {
    //登录功能
    $scope.login = function() {
        var username = $scope.login_username;
        var password = $scope.login_password;
        if(username==null || username=="" || password==null || password=="")
            alert("请输入用户名密码！");
        else{
            $http({
                method:"post",
                url: ip + "login.php",
                data:{
                    username: username,
                    password: password
                }
            }).success(function(data, status, headers, config){
                if (data.success) {
                    //登录成功
                    alert("登录成功");
                    window.location.href="publish.html";
                }else{
                    alert("登录失败: " + data.msg);
                }
            })
        }
    }
});